/**
 * File Name   : PortalProductsAction.java
 * Description : 
 */
package com.hh.portal.products.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.service.CommonService;
import com.hh.admin.producttype.form.ParamProductTypeBean;
import com.hh.admin.producttype.form.ResultProductTypeBean;
import com.hh.admin.producttype.service.ProductTypeService;
import com.hh.common.bean.PortalLocalBean;
import com.hh.portal.products.form.ParamPortalProductsBean;
import com.hh.portal.products.form.PortalProductForm;
import com.hh.portal.products.form.ResultPortalProductsBean;

/**
 * 首页-产品与服务列表
 * @author 冯昌硕
 * @create 2013-2-3 下午10:01:37
 */
public class PortalProductsAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(PortalProductsAction.class);
    
    private CommonService commonService;
    
    private ProductTypeService producttypeService;
    
    /**
     * 各产品类型混合查询
     * @create 2013-2-3 下午10:16:15
     * @modify 2013-02-16 16:19 前台-产品与服务：点击左侧产品列表项，查找指定类型的产品内容记录
     */
    public ActionForward allTypeList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("产品与服务");
        firLocalBean.setHref("./products.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        // 查询产品类型列表
        PortalProductForm pform = (PortalProductForm) form;
        ParamProductTypeBean paramBean = new ParamProductTypeBean();
        List<ResultProductTypeBean> producttypeLst = 
                commonService.selRecordList(pform, "producttype.selPortalProductTypeList", paramBean);
        request.setAttribute("producttypeLst", producttypeLst);
        
        int productTypeId = pform.getProductTypeId();
        
        // 查询产品内容列表
        ParamPortalProductsBean portalParamBean = new ParamPortalProductsBean();
        if(productTypeId != 0) {
            portalParamBean.setProductTypeId(productTypeId);
        } else {
            String typeFlag = pform.getTypeFlag();
            if((null != typeFlag) && (!"".equals(typeFlag))) {
                productTypeId = producttypeService.getProductTypeIdByFlag(typeFlag);
                portalParamBean.setProductTypeId(productTypeId);
            }
        }
        
        List<ResultPortalProductsBean> portalProductsLst =
                commonService.selRecordList(pform, "commonProduct.selCommonProductList", portalParamBean);
        request.setAttribute("portalProductsLst", portalProductsLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(portalParamBean, "commonProduct.selCommonProductCount");
        pform.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / pform.getPageSize();
        if(totleCnt % pform.getPageSize() != 0) {
            pageCnt += 1;
        }
        pform.setTotlePage(pageCnt);
        log.debug("查询综合产品记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("products");
    }
    
    /**
     * 产品与服务界面-点击产品链接进入产品详情界面
     * @create 2013-2-22 下午8:48:11
     */
    public ActionForward productDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("产品与服务");
        firLocalBean.setHref("./products.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        // 查询产品类型列表
        PortalProductForm pform = (PortalProductForm) form;
        int productTypeId = pform.getProductTypeId(); // 产品类型ID
        int contentId = pform.getProductContentId();  // 产品内容ID
        
        // 根据产品类型ID查询产品类型标识
        String flag = producttypeService.getProductFlagById(productTypeId);
        log.debug("[productDetail] productTypeId=["+productTypeId+"] contentId=[" + contentId + "] productTypeFlag=[" + flag + "]");
        
        String destPath = mapping.findForward(flag).getPath();
        ActionForward forward = new ActionForward(destPath + "&id=" + contentId);
        return forward;
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    public void setProducttypeService(ProductTypeService producttypeService) {
        this.producttypeService = producttypeService;
    }
    
}
